# -*- coding: utf-8 -*-
"""
Created on Thu Dec  3 18:55:10 2020
find element2
find_element_by_css_selector
@author: andrew
"""

import time

from selenium import webdriver

driver = webdriver.Chrome('./diver/chromedriver')

try:
    driver.get("http://www.python.org")
    driver.set_window_size(1190, 690)
    # time.sleep(1)
    print(driver.title)
    # <button type="submit" name="submit" id="submit" class="search-button" title="Submit this Search" tabindex="3">
    #                                     GO
    #                                 </button>
    # 1 通过id属性定位元素
    #  #号表示通过id属性来定位元素
    # element = driver.find_element_by_css_selector("#submit")
    # 2 通过class属性定位元素
    # .号表示通过class属性来定位元素
    # element = driver.find_element_by_css_selector(".search-button")
    # 3 通过标签名定位元素
    # element = driver.find_element_by_css_selector("button")
    # 4 通过属性定位元素  属性值包含某个值 适用于由空格分隔的属性值
    # element = driver.find_element_by_css_selector("[name='submit']")
    # element = driver.find_element_by_css_selector("[tabindex='3']")
    # element = driver.find_element_by_css_selector("[title='Submit this Search']")
    # element = driver.find_element_by_css_selector("[title~='this']")
    # 5 父子定位元素  父元素的标签名为fieldset，button的子元素
    # <fieldset title="Search Python.org">
    #                                 <span aria-hidden="true" class="icon-search"></span>
    #                                 <label class="screen-reader-text" for="id-search-field">Search This Site</label>
    #                                 <input id="id-search-field" name="q" type="search" role="textbox" class="search-field" placeholder="Search" value="" tabindex="1">
    #                                 <button type="submit" name="submit" id="submit" class="search-button" title="Submit this Search" tabindex="3">
    #                                     GO
    #                                 </button>
    #                                 <!--[if IE]><input type="text" style="display: none;" disabled="disabled" size="1" tabindex="4"><![endif]-->
    # </fieldset>
    # element = driver.find_element_by_css_selector("fieldset>button")
    # 6 组合定位元素  标签名#id属性值：指的是该input标签下id属性为kw的元素,标签名.class属性值：指的是该标签下class属性的元素
    # <button type="submit" name="submit" id="submit" class="search-button" title="Submit this Search" tabindex="3">
    #                                     GO
    #                                 </button>
    # element = driver.find_element_by_css_selector("button#submit")
    # element = driver.find_element_by_css_selector("button.search-button")
    # 7 组合复杂定位元素 标签名[属性=’属性值‘]
    # element = driver.find_element_by_css_selector("button[class='search-button']")
    # 8 父子属性值定位元素  父元素的标签名为fieldset，button的子元素
    element = driver.find_element_by_css_selector("fieldset>button.search-button")
    # 9 标签多个属性组合定位元素
    # element = driver.find_element_by_css_selector("button.search-button[name='submit']")
    # element = driver.find_element_by_css_selector("button[id='submit'][name='submit']")
    print("id:", element.get_attribute("id"))
    print("name:", element.get_property("name"))
    print("class:", element.get_attribute("class"))
    print("Text:", element.text)
    element.click()
    time.sleep(1)
except Exception as e:
    print('Exception msg: ', e)
finally:
    driver.quit()
